package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.PatientMedicalRecordMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.PatientMedicalRecordDto;
import com.mk.admin.model.dto.PatientMedicalRecordRemoteDto;
import com.mk.admin.model.entity.PatientMedicalRecordEntity;
import com.mk.admin.model.vo.PatientMedicalRecordRemoteVo;
import com.mk.admin.model.vo.PatientMedicalRecordVo;
import com.mk.admin.service.PatientMedicalRecordService;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class PatientMedicalRecordServiceImpl implements PatientMedicalRecordService {
    @Autowired
    private PatientMedicalRecordMapper patientMedicalRecordMapper;

    @Override
    public ResultDto select(PatientMedicalRecordVo patientMedicalRecordVo) {
        PatientMedicalRecordEntity entity = JSONCopy.copy(patientMedicalRecordVo, PatientMedicalRecordEntity.class);
        return ResultDto.data(patientMedicalRecordMapper.count(entity), patientMedicalRecordMapper.select(entity));
    }

    @Override
    public ResultDto remote(PatientMedicalRecordRemoteVo patientMedicalRecordRemoteVo) {
        JSONObject jsonObject = HttpTool.postJson("emr_1001", JSONObject.toJSONString(patientMedicalRecordRemoteVo));
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }

    @Override
    public ResultDto add(PatientMedicalRecordVo patientMedicalRecordVo) {
        PatientMedicalRecordEntity entity = JSONCopy.copy(patientMedicalRecordVo, PatientMedicalRecordEntity.class);
        return ResultDto.data(patientMedicalRecordMapper.insert(entity));
    }

    @Override
    public ResultDto update(PatientMedicalRecordVo patientMedicalRecordVo) {
        PatientMedicalRecordEntity entity = JSONCopy.copy(patientMedicalRecordVo, PatientMedicalRecordEntity.class);
        return ResultDto.data(patientMedicalRecordMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(patientMedicalRecordMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(patientMedicalRecordMapper.status(id));
    }

    /**
     * 每次都会刷新一次，如果不传卡号，则刷新所有，否则刷新当前患者信息的门诊病历
     *
     * @param patientMedicalRecordRemoteVo
     * @return
     */
    @Override
    public ResultDto report(PatientMedicalRecordRemoteVo patientMedicalRecordRemoteVo) {
<<<<<<< HEAD
        JSONObject jsonObject = HttpTool.postJson("emr_1001", JSONObject.toJSONString(patientMedicalRecordRemoteVo));
=======
        JSONObject jsonObject = HttpTool.postJson("com_0326", JSONObject.toJSONString(patientMedicalRecordRemoteVo));
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
        Object result = jsonObject.get("result");
        if (result != null && !"[]".equals(result)) {
            List<PatientMedicalRecordRemoteDto> list = JSONArray.parseArray(result.toString(), PatientMedicalRecordRemoteDto.class);
            if (list.size() > 0) {
//              将得到的数据和数据库里面进行对比，有改动的就修改，没有改动的就增加
                list.forEach(item -> {
                    PatientMedicalRecordEntity entity = new PatientMedicalRecordEntity();
                    entity.setEmrCode(item.getEmrCode());
                    List<PatientMedicalRecordDto> select = patientMedicalRecordMapper.select(entity);
                    entity = JSONCopy.copy(item, PatientMedicalRecordEntity.class);
                    entity.setDoctorName(item.getAttendingPhysician());
                    entity.setDepartmentCode(item.getVisitDeptCode());
                    entity.setDepartmentName(item.getVisitDeptName());
                    entity.setInDeptCode(item.getAdmissionCode());
                    entity.setInDeptName(item.getAdmissionName());
                    entity.setPreHistory(item.getPastDiseaseHistory());
                    entity.setMarryHistory(item.getObstetricalHistory());
                    if (select.size() > 0) {
                        entity.setId(select.get(0).getId());
                        patientMedicalRecordMapper.updateById(entity);
                    } else {
                        patientMedicalRecordMapper.insert(entity);
                    }
                });
            }
        }
        if (patientMedicalRecordRemoteVo.getCardNo() != null) {
            PatientMedicalRecordEntity entity = new PatientMedicalRecordEntity();
            entity.setPatId(patientMedicalRecordRemoteVo.getCardNo());
            entity.setOrderBy("record_time desc");
            return ResultDto.data(patientMedicalRecordMapper.count(entity), patientMedicalRecordMapper.select(entity));
        }
        return ResultDto.data(Collections.emptyList());
    }
}
